﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace KgUtils.Web
{
    /// <summary>
    /// This CacheHelper is only valid for the CURRENT request.
    /// Its content is reset between each request.
    /// See http://www.4guysfromrolla.com/articles/060904-1.aspx
    /// </summary>
    public static partial class RequestCacheHelper
    {
        public static void SetCache(string key, object value)
        {
            HttpContext.Current.Items[key] = value;
        }

        public static bool IsDefined(string key)
        {
            return HttpContext.Current.Items.Contains(key);
        }

        public static T GetCache<T>(string key)
        {
            if (HttpContext.Current.Items.Contains(key))
                return (T)HttpContext.Current.Items[key];
            else
                return default(T);
        }

        public static void ClearCache(string key)
        {
            HttpContext.Current.Items.Remove(key);
        }

        public static void ClearCache()
        {
            HttpContext.Current.Items.Clear();
        }
    }
}
